<?php 
//OJB: Check if for excel export process
if($export_excel == 1){
	ob_start();
	$this->load->view("partial/header_excel");
}else{
	$this->load->view("partial/header");
} 



function renderData($param, $_this){
    $str = '';
    
    foreach($param as $row){
       $str .= '<tr>';
         //$str .= '<td height="27"><b>'.$row['name'].'</b></td>';
         $str .= '<td colspan="13" height="27"><b>'.$row['date'].'</b></td>';
       $str .= '</tr>';
       $str .= '<tr>';
         $str .= '<td height="33">&nbsp;</td>';
         $str .= '<td colspan="3" align="center"><b>Stocks Opening</b></td>';
         $str .= '<td colspan="3" align="center"><b>Sales</b></td>';
         $str .= '<td colspan="3" align="center"><b>Returns</b></td>';
         $str .= '<td colspan="3" align="center"><b>Balance</b></td>';
       $str .= '</tr>';
       $str .= '<tr>';
         $str .= '<td height="33"><b>Items</b></td>';
         $str .= '<td><b>Quantity</b></td>';
         $str .= '<td><b>Unit Price</b></td>';
         $str .= '<td><b>Total '.$_this->config->item('currency_inuse').'</b></td>';
         $str .= '<td><b>Quantity</b></td>';
         $str .= '<td><b>Unit Price</b></td>';
         $str .= '<td><b>Total '.$_this->config->item('currency_inuse').'</b></td>';
         $str .= '<td><b>Quantity</b></td>';
         $str .= '<td><b>Unit Price</b></td>';
         $str .= '<td><b>Total '.$_this->config->item('currency_inuse').'</b></td>';
         $str .= '<td><b>Quantity</b></td>';
         $str .= '<td><b>Unit Price</b></td>';
         $str .= '<td><b>Total '.$_this->config->item('currency_inuse').'</b></td>';
       $str .= '</tr>';
       
       $stockTotals = 0;
       $salesTotals = 0;
       $returnTotals = 0;
       $balanceTotals = 0;
       foreach($row['data'] as $inner_row){
            $item_name = $inner_row['name']; 
            $item_quantity = intval($inner_row['item_quantity']);
            $item_unitprice = to_currency_no_money($inner_row['item_unitprice']);
            $sales_quantity = intval($inner_row['sales_quantity']);
            $returns_quantity = intval($inner_row['returns_quantity']);
            $balance_quantity = ($item_quantity + $returns_quantity) - $sales_quantity;
            $time_quantity = $inner_row['time_quantity'];
            
            $stock_rtl = to_currency_no_money($item_quantity * $item_unitprice);
            $sales_rtl = to_currency_no_money($sales_quantity * $item_unitprice);
            $returns_rtl = to_currency_no_money($returns_quantity * $item_unitprice);
            $balance_rtl = to_currency_no_money($balance_quantity * $item_unitprice);
            
            $stockTotals += $stock_rtl;
            $salesTotals += $sales_rtl;
            $returnTotals += $returns_rtl;
            $balanceTotals += $balance_rtl;
            
            $str .= '<tr>';
                $str .= '<td height="33">'.$item_name.'</td>';
                $str .= '<td>'.$item_quantity.'</td>';
                $str .= '<td>'.$item_unitprice.'</td>';
                $str .= '<td>'.$stock_rtl.'</td>';
                $str .= '<td>'.$sales_quantity.'</td>';
                $str .= '<td>'.$item_unitprice.'</td>';
                $str .= '<td>'.$sales_rtl.'</td>';
                $str .= '<td>'.$returns_quantity.'</td>';
                $str .= '<td>'.$item_unitprice.'</td>';
                $str .= '<td>'.$returns_rtl.'</td>';
                $str .= '<td>'.$balance_quantity.'</td>';
                $str .= '<td>'.$item_unitprice.'</td>';
                $str .= '<td>'.$balance_rtl.'</td>';
             $str .= '</tr>';
       }
       
        $str .= '<tr>';
         $str .= '<td height="33">&nbsp;</td>';
         $str .= '<td>&nbsp;</td>';
         $str .= '<td>&nbsp;</td>';
         $str .= '<td><b>'.to_currency_no_money($stockTotals).'</b></td>';
         $str .= '<td>&nbsp;</td>';
         $str .= '<td>&nbsp;</td>';
         $str .= '<td><b>'.to_currency_no_money($salesTotals).'</b></td>';
         $str .= '<td>&nbsp;</td>';
         $str .= '<td>&nbsp;</td>';
         $str .= '<td><b>'.to_currency_no_money($returnTotals).'</b></td>';
         $str .= '<td>&nbsp;</td>';
         $str .= '<td>&nbsp;</td>';
         $str .= '<td><b>'.to_currency_no_money($balanceTotals).'</b></td>';
       $str .= '</tr>';
       $str .= '<tr>';
         $str .= '<td colspan="13">&nbsp;</td>';
       $str .= '</tr>';
    }
    
    return $str;
}

//echo '<pre>';
//print_r($report_data);
//echo '</pre>';



?>
<div id='menu_print' align='left'>
<?php //echo anchor("javascript: void(0);",
	//	"<img align='absbottom' src=".base_url().'images/printmgr.gif'." border='0' alt='Print' title='Print' height='30px' width='30px'>",
	//	array('class'=>'none', 'id'=>'print_btn', 'onclick'=>'window.print();'));
		?>
</div>
<div id="page_title" style="margin-bottom:8px;"><?php echo $title ?></div>
<div id="page_subtitle" style="margin-bottom:8px;"><?php echo $subtitle ?></div>
<div id="table_holder">
    <table class="tablesorter report" id="sortable_table" border="1">
		<tbody>
            <?php
        	   if($report_data){
        	       echo renderData($report_data, $this);
        	   }
               else{
                   // echo 'no';
               }
            ?>
        </tbody>
	</table>
</div>
<div id="report_summary">

</div>
<?php 
if($export_excel == 1){
	$this->load->view("partial/footer_excel");
	$content = ob_end_flush();
	
	$filename = trim($filename);
	$filename = str_replace(array(' ', '/', '\\'), '', $title);
	$filename .= "_Export.xls";
	header('Content-type: application/ms-excel');
	header('Content-Disposition: attachment; filename='.$filename);
	echo $content;
	die();
	
}else{
	$this->load->view("partial/footer"); 
?>
<script type="text/javascript" language="javascript">
$(document).ready(function()
{
	$(".tablesorter a.expand").click(function(event)
	{
		$(event.target).parent().parent().next().find('.innertable').toggle();
		
		if ($(event.target).text() == '+')
		{
			$(event.target).text('-');
		}
		else
		{
			$(event.target).text('+');
		}
		return false;
	});
	
});
</script>
<?php 
} // end if not is excel export 
?>